package com.ailk.kbi.kbirazor.control
{
	import com.ailk.kbi.kbirazor.common.Util;
	import com.ailk.kbi.kbirazor.model.AreaModel;
	import com.ailk.kbi.kbirazor.model.BrandModel;
	import com.ailk.kbi.kbirazor.vo.KpiVO;
	import com.ailk.kbi.kbirazor.vo.ResultVO;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import spark.formatters.NumberFormatter;

	public class BrandControl
	{
		internal var _service:RemoteObject;       		
		internal var brandModel:BrandModel = BrandModel.getInstance();

		
		private var intFormat:NumberFormatter = new NumberFormatter();
		private var doubleFormat:NumberFormatter = new NumberFormatter();
		
		public function BrandControl()
		{
			_service = new RemoteObject();            	
			_service.destination = "flexService";
			_service.endpoint = "messagebroker/amf";
			_service.showBusyCursor = true;
			_service.addEventListener(FaultEvent.FAULT, Util.faultHandler);
			_service.getBrandDataList.addEventListener(ResultEvent.RESULT, getBrandDataListHandler);
			
		}
		
		public function getBrandDataList():void {
			_service.getBrandDataList(brandModel.opTime, brandModel.kpiIdList); 
		}
		private function getBrandDataListHandler(event:ResultEvent):void{
			try {
				var result:ResultVO =  _service.getBrandDataList.lastResult;	
				var xml:XML = new XML(event.result.xmlString);
				xml = parseXML(xml,brandModel.kpiIdList);
				brandModel.dataXML = xml;
			} catch(e:Error) {
				
			}
		}
		
		
//		public function cityList():void {
//			_service.cityList(areaModel.dimVO, areaModel.kpiIdList); 
//		}
//		private function cityListHandler(event:ResultEvent):void{
//			try {
//				var result:ResultVO =  _service.cityList.lastResult;	
//				var xml:XML = new XML(event.result.xmlString);
//				xml = parseXML(xml,areaModel.kpiIdList);
//				for each(var childXML:XML in xml.data){
//					areaModel.selectedXML.appendChild(childXML);
//				}
//			} catch(e:Error) {
//				
//			}
//		}
		
		private function parseXML(xml:XML,kpiIdList:ArrayCollection):XML {
			var kpiVOCount:int = brandModel.kpiVOList.length;
			var division_flag:int = 0;
			
			intFormat.fractionalDigits = 0;
			intFormat.useGrouping = false;
			
			doubleFormat.fractionalDigits = 2;
			doubleFormat.useGrouping = false;
			
			var xmlList:XMLList = xml.data;
			
			var arr:Array = new Array();
			arr.push("brandName");
			
			for(var index:String in kpiIdList) {
				arr.push("kpiValue_"+kpiIdList.getItemAt(Number(index)));
			}
			for(var i:int=0; i<xmlList.length(); i++) {
				var oneXml:XML = xmlList[i];
				var attriList:XMLList = oneXml.children();
				for(var j:int=0; j<attriList.length(); j++) {
					division_flag = 0;
					
					for(var kc:int = 0; kc<kpiVOCount; kc++) {
						if(arr[j] == ("kpiValue_"+(KpiVO)(brandModel.kpiVOList.getItemAt(kc)).kpiId)) {
							division_flag = (KpiVO)(brandModel.kpiVOList.getItemAt(kc)).divisionFlag;
							break;
						}
					}
					if(division_flag == 1) {
						oneXml.@[arr[j]] = intFormat.format(attriList[j].toString());
					} else if(division_flag == 2) {
						oneXml.@[arr[j]] = doubleFormat.format(attriList[j].toString());
					} else if(division_flag == 3) {
						oneXml.@[arr[j]] = doubleFormat.format(attriList[j].toString()) + "%";
					} else {
						oneXml.@[arr[j]] = attriList[j].toString();
					}
					
				}
				
				var xmlCollection:XMLListCollection = new XMLListCollection(attriList);
				xmlCollection.removeAll();
			}
			return xml;
		}
	}
}